package com.sgb.kjwl.view.ui.activity.self.goods;


import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jyb.qiyefuwu.uitls.DialogFunction;
import com.jyb.qiyefuwu.uitls.DialogHelper;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityAddMemberGoodsBinding;
import com.sgb.kjwl.model.entity.ItemSkuMemberPriceDTOS;
import com.sgb.kjwl.model.entity.SkuDetail;
import com.sgb.kjwl.model.entity.CommitVipPrice;
import com.sgb.kjwl.view.event.self.VipPriceEvent;
import com.sgb.kjwl.view.ui.adapter.self.goods.MemberPriceAdapter;
import com.sgb.kjwl.view.widget.self.goods.SetMemberPricePopWindow;
import com.sgb.kjwl.viewmodel.self.goods.AddMemberGoodsModel;
import com.sgb.link.router.PathGoods;
import com.swgk.core.common.base.BaseActivity;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;


/**
 * 作者:陈方凯
 * 日期:2021/11/15
 * 说明:新增会员报价
 */
@Route(path = PathGoods.AddMemberGoodsActivity)
public class AddMemberGoodsActivity extends BaseActivity<AddMemberGoodsModel, ActivityAddMemberGoodsBinding> {
    private MemberPriceAdapter mAdapter;
    private SetMemberPricePopWindow mPopWindow;
    private String itemId = "";
    private String oldItemMemberId = "";
    private int code;

    @Override
    protected int layoutId() {
        return R.layout.activity_add_member_goods;
    }

    @Override
    protected void createViewModel() {
        mViewModel = new ViewModelProvider(this).get(AddMemberGoodsModel.class);
        mBinding.setModel(mViewModel);
    }

    @Override
    protected void init() {
        itemId = getIntent().getStringExtra("itemId");
        oldItemMemberId = getIntent().getStringExtra("oldItemMemberId");
        mBinding.backView.setOnClickListener(this);
        mBinding.btCommit.setOnClickListener(this);
        initView();
        initObserver();
        mViewModel.getSkuList(itemId);
    }

    private void initView() {
        mAdapter = new MemberPriceAdapter(this, null);
        mAdapter.setItemSelect(false);
        mBinding.rvPriceList.setLayoutManager(new LinearLayoutManager(this));
        mBinding.rvPriceList.setAdapter(mAdapter);
        mAdapter.setOnItemClick(new AdapterOnItemClick<SkuDetail.SkuListEntity>() {
            @Override
            public void onItemClick(SkuDetail.SkuListEntity s, int position) {
                mPopWindow.setData(s);
                mPopWindow.showDownPopwindow(mBinding.grayLayout, true);
            }
        });

        if (mPopWindow == null) {
            mPopWindow = new SetMemberPricePopWindow(AddMemberGoodsActivity.this);
        }
        mPopWindow.setBgView(mBinding.grayLayout);
        mPopWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        mPopWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        mPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                SkuDetail.SkuListEntity skuListEntity = (SkuDetail.SkuListEntity) key1;
                code = 0;
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    private void initObserver() {
        mViewModel.skuList.observe(this, new Observer<List<SkuDetail.SkuListEntity>>() {
            @Override
            public void onChanged(List<SkuDetail.SkuListEntity> skuListEntities) {
                if (skuListEntities != null)
                    if (code == 50005) {
                        for (int i = 0; i < skuListEntities.size(); i++) {
                            //比对促销价格或者销售价格是否不一样
                            if (!skuListEntities.get(i).getPromotePrice().equals(mAdapter.getDatas().get(i).getPromotePrice()) || !skuListEntities.get(i).getMoney().equals(mAdapter.getDatas().get(i).getMoney())) {
                                mAdapter.getDatas().get(i).setMoney(skuListEntities.get(i).getMoney());
                                mAdapter.getDatas().get(i).setPromotePrice(skuListEntities.get(i).getPromotePrice());
                                mAdapter.getDatas().get(i).setSet(false);
                                mAdapter.getDatas().get(i).setChange(true);
                            }
                        }
                        mAdapter.notifyDataSetChanged();
                    } else {
                        mAdapter.setDatas(skuListEntities);
                    }
            }
        });
        mViewModel.status.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                //50002 企业注销
                //50003 下架删除
                //50004 sku变化
                //50001 有会员价格异常code码
                if (integer == 200) {
                    EventBus.getDefault().post(new VipPriceEvent().setMessageTag(integer));
                    finish();
                } else if (integer == 50002) {
                    DialogHelper.showDialog(AddMemberGoodsActivity.this,
                            "温馨提示",
                            "操作失败！企业已注销，店铺关闭",
                            "",
                            "知道了",
                            new DialogFunction() {
                                @Override
                                public void normalRoundDialog() {
                                }
                            });
                } else if (integer == 50003) {
                    DialogHelper.showDialog(AddMemberGoodsActivity.this,
                            "温馨提示",
                            "操作失败！当前商品已下架/不存在",
                            "",
                            "知道了",
                            new DialogFunction() {
                                @Override
                                public void normalRoundDialog() {
                                    EventBus.getDefault().post(new VipPriceEvent().setMessageTag(integer));
                                    finish();
                                }
                            });
                } else if (integer == 50004) {
                    DialogHelper.showDialog(AddMemberGoodsActivity.this,
                            "温馨提示",
                            "操作失败！当前商品发生变化",
                            "",
                            "知道了",
                            new DialogFunction() {
                                @Override
                                public void normalRoundDialog() {
                                    EventBus.getDefault().post(new VipPriceEvent().setMessageTag(integer));
                                    finish();
                                }
                            });
                } else if (integer == 50005) {
                    code = 50005;
                    DialogHelper.showDialog(AddMemberGoodsActivity.this,
                            "温馨提示",
                            "操作失败！当前商品价格发生变化",
                            "",
                            "知道了",
                            new DialogFunction() {
                                @Override
                                public void normalRoundDialog() {
                                    mViewModel.getSkuList(itemId);
                                }
                            });
                } else if (integer == 50001) {
                    code = 50001;
                    DialogHelper.showDialog(AddMemberGoodsActivity.this,
                            "温馨提示",
                            "操作失败！当前商品已在其他端设置成功",
                            "",
                            "知道了",
                            new DialogFunction() {
                                @Override
                                public void normalRoundDialog() {
                                    EventBus.getDefault().post(new VipPriceEvent().setMessageTag(integer));
                                    finish();
                                }
                            });
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.back_view) {
            DialogHelper.showDialog(AddMemberGoodsActivity.this,
                    "温馨提示",
                    "此操作将丢失当前表单填写的内容，您还要继续吗？",
                    "取消",
                    "确定",
                    new DialogFunction() {
                        @Override
                        public void normalRoundDialog() {
                            finish();
                        }
                    });
        } else if (id == R.id.bt_commit) {
            checkData();
        }
    }

    private void checkData() {
        CommitVipPrice commitVipPrice = new CommitVipPrice();
        List<ItemSkuMemberPriceDTOS> list = new ArrayList<>();
        commitVipPrice.setItemId(itemId);
        if (!TextUtils.isEmpty(oldItemMemberId))
            commitVipPrice.setOldItemMemberId(oldItemMemberId);
        for (int i = 0; i < mAdapter.getDatas().size(); i++) {
            if (!mAdapter.getDatas().get(i).isSet()) {
                MToast.showToast(AddMemberGoodsActivity.this, "请填写会员价");
                return;
            }
            for (int j = 0; j < 6; j++) {
                ItemSkuMemberPriceDTOS skuMemberPriceDTOS = new ItemSkuMemberPriceDTOS();
                skuMemberPriceDTOS.setItemId(itemId);
                skuMemberPriceDTOS.setLevel(j);
                skuMemberPriceDTOS.setParityPrice(mAdapter.getDatas().get(i).getMoney());
                skuMemberPriceDTOS.setPromotionPrice(mAdapter.getDatas().get(i).getPromotePrice());
                skuMemberPriceDTOS.setSkuDetails(mAdapter.getDatas().get(i).getAttr());
                skuMemberPriceDTOS.setSkuId(mAdapter.getDatas().get(i).getId());
                switch (j) {
                    case 0:
                        skuMemberPriceDTOS.setPrice(mAdapter.getDatas().get(i).getPrice());
                        break;
                    case 1:
                        skuMemberPriceDTOS.setPrice(mAdapter.getDatas().get(i).getPrice1());
                        break;
                    case 2:
                        skuMemberPriceDTOS.setPrice(mAdapter.getDatas().get(i).getPrice2());
                        break;
                    case 3:
                        skuMemberPriceDTOS.setPrice(mAdapter.getDatas().get(i).getPrice3());
                        break;
                    case 4:
                        skuMemberPriceDTOS.setPrice(mAdapter.getDatas().get(i).getPrice4());
                        break;
                    case 5:
                        skuMemberPriceDTOS.setPrice(mAdapter.getDatas().get(i).getPrice5());
                        break;
                }
                list.add(skuMemberPriceDTOS);
            }
        }
        commitVipPrice.setItemSkuMemberPriceDTOS(list);
        mViewModel.commitData.setValue(commitVipPrice);
        mViewModel.commitVipPrice();
    }
}
